package com.jinlia.show.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jinlia.show.modules.base.system.entity.Goods;
import com.jinlia.show.modules.base.system.entity.GoodsLabel;
import com.jinlia.show.modules.base.system.entity.GoodsLabelRel;
import com.jinlia.show.modules.base.model.form.GoodsForm;
import com.jinlia.show.modules.base.model.po.GoodsPO;
import com.jinlia.show.modules.base.model.query.GoodsQuery;
import com.jinlia.show.modules.base.system.service.GoodsLabelMapperService;
import com.jinlia.show.modules.system.service.GoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 物料管理
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("goods")
public class GoodsController {
    final GoodsService goodsService;
    @Autowired
    GoodsLabelMapperService goodsLabelMapperService;

    @PostMapping("/page")
    public ResponseEntity<IPage<GoodsPO>> getPage(@RequestBody GoodsQuery goodsQuery) {
        //获取商品
        if (StringUtils.hasLength(goodsQuery.getGoodsLabelTitle())) {
            GoodsLabel one = goodsLabelMapperService.lambdaQuery()
                    .eq(GoodsLabel::getTitle, goodsQuery.getGoodsLabelTitle())
                    .select(GoodsLabel::getId)
                    .one();
            if (one != null) {
                goodsQuery.setGoodsLabelId(one.getId());
            }

        }
        IPage<GoodsPO> page = goodsService.getPage(goodsQuery);
        page.getRecords().forEach(i -> {
            //填充商品分类
            i.filedVO(i::getGoodsTypeName);
        });
        return ResponseEntity.ok(page);
    }

    @GetMapping("/get/{id}")
    public ResponseEntity<GoodsPO> getById(@PathVariable("id") Long id) {
        GoodsForm goodsForm = new GoodsForm();
        goodsForm.setGoodsId(id);
        List<GoodsPO> forOrder = goodsService.getForOrder(goodsForm);
        return ResponseEntity.ok(forOrder.get(0));
    }

    /**
     *
     */
    @PostMapping("/order/list")
    public ResponseEntity<List<GoodsPO>> readGoodsTemp(@RequestBody GoodsForm request) {
        List<GoodsPO> forOrder = goodsService.getForOrder(request);
        return ResponseEntity.ok(forOrder);
    }

    @PostMapping("saveOrUp")
    public ResponseEntity<GoodsPO> saveOrUp(@RequestBody GoodsPO request) {
        goodsService.saveOrUpPOList(Arrays.asList(request));
        return ResponseEntity.ok(request);
    }

    /**
     * 导入物料
     *
     * @param file
     */
    @PostMapping("import/temp2")
    public void readGoodsTemp2(@RequestParam(value = "file") MultipartFile file) {
        goodsService.importGoodsTemp2(file);
    }

    /**
     * 导出物料
     */
    @PostMapping("export2")
    public void exportGoods2(@RequestBody GoodsQuery goodsQuery, HttpServletResponse response) {
        goodsService.exportGoods2(goodsQuery,response);
    }

    /**
     * 绑定标签
     *
     * @param goodsTagRelList
     */
    @PostMapping("bind/tags/{tagId}")
    public void bindTags(@PathVariable("tagId") Long tagId ,@RequestBody List<GoodsLabelRel> goodsTagRelList) {
        goodsService.bindTags(tagId, goodsTagRelList);
    }
    /**
     * 禁用商品
     */
    @PostMapping("disable")
    public void disable(@RequestBody Goods request) {
        goodsService.lambdaUpdate()
                .set(Goods::getEnableStatus, request.getEnableStatus())
                .eq(Goods::getId, request.getId())
                .update();
    }
}
